package net.esj.basic.utils.writer.json;

import net.esj.basic.dao.querybuilder.QueryBuilder;

import org.hibernate.collection.internal.PersistentBag;
import org.hibernate.collection.internal.PersistentSet;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;

public class HibenateJsonWriter extends JsonWriter {

	@Override
	protected Object proxyCheck(Object bean) {
		if(bean instanceof HibernateProxy) {
            LazyInitializer lazyInitializer = ((HibernateProxy)bean).getHibernateLazyInitializer();
            //if(lazyInitializer.isUninitialized()) {//不管有没有初始化都要返回ID，否则会死循环
//                System.out.println(">>>>>lazyInitializer.getIdentifier()="+ lazyInitializer.getIdentifier());
            return lazyInitializer.getIdentifier();
            //}
        }
        if(bean instanceof LazyInitializer){
        	LazyInitializer lazyInitializer= (LazyInitializer) bean;
        	return lazyInitializer.getIdentifier();
        }
        if(bean instanceof PersistentSet) {
            return new String[] {}; //忽略hibernate one-to-many
        }
        if(bean instanceof PersistentBag){
        	return new String[]{};
        }
        if(bean instanceof QueryBuilder){
        	return "";
        }
        return super.proxyCheck(bean);
	}

	
}
